package beyjava.model;

import java.awt.Desktop;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import beyjava.util.Common;
import beyjava.util.CsvWriter;
import beyjava.util.ExcelWriter;

/**
 * @author BEYJAVA
 *
 */
public class Report {
    
	private String[] projectCsvHeader;
	private String[] activityCsvHeader;
	private String[] taskCsvHeader;
	private String[] resourceCsvHeader;
	private String[][] data;
	private String fileName;
	/**
	 * the initilizing csv and project report
	 * @param fileName the name of report file
	 */
	public Report(String fileName){
		projectCsvHeader = new String[]{"projectID","projectName", "projectDesc","projectStartDate"};
		activityCsvHeader = new String[]{"actNumber","actDesc","actStartDate","actDeliver","hour","projectID"};
		taskCsvHeader = new String[]{"taskNumber","taskDesc","taskStartDate","taskHours","activityID"};
		resourceCsvHeader = new String[]{"resourceID" ,"personName","personGender","resourceType","taskID"};
		this.fileName = fileName;
	}

	/**
	 * This method provide reporting the all projectDetails in csv format
	 * @param projects
	 * @param activies
	 * @param tasks
	 *
	 */
	public void reportProjectToCSV(Vector<Project> projects,Vector<Activity> activies ,Vector<Task> tasks){
		CsvWriter csvWriter = null;
		try {
			csvWriter = new CsvWriter(this.fileName);
			csvWriter.writeCSVHeader(this.projectCsvHeader);
			int i = 0;
			data = new String[projects.size()][this.projectCsvHeader.length];
			for(Project project : projects){
				String[] projectRow = projectToCsvFormat(project);
				this.data[i] = projectRow;
				i++;
			}
			csvWriter.writeCSVContent(data);
			this.data = new String[activies.size()][this.activityCsvHeader.length];
			csvWriter.writeCSVHeader(this.activityCsvHeader);
			i = 0;
			for(Activity activity : activies){
				String[] actaivityRow = activityToCsvFormat(activity);
				this.data[i] = actaivityRow;
				i++;
			}
			i = 0;
			csvWriter.writeCSVContent(data);
			this.data = new String[tasks.size()][this.taskCsvHeader.length];
			csvWriter.writeCSVHeader(this.taskCsvHeader);
			for(Task task : tasks){
				String[] taskRow = taskToCsvFormat(task);
				this.data[i] = taskRow;
				i++;
			}
			i = 0;
			csvWriter.writeCSVContent(data);
			Vector<String[]> dataVector = new Vector<String[]>();
			csvWriter.writeCSVHeader(this.resourceCsvHeader);
			for(Task task : tasks){
				Vector<Person> taskResources = task.getTaskResource();
				for(Person taskResource: taskResources){
					String[] resourceRow = resourceToCsv(taskResource, task);
					dataVector.add(resourceRow);
				}
			}
			
			csvWriter.writeCSVContent(dataVector);
			csvWriter.closeCSVWriter();
			Desktop desktop = Desktop.getDesktop();
			File newFile = new File(fileName);
			desktop.open(newFile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}
		
	}
	/**This method provides reporting the all projects details in excel;
	 * @param projects
	 */
	public void reportProjectToExcel(Vector<Project> projects){
		 try {
			ExcelWriter excelWriter = new ExcelWriter(this.fileName);
			
			for(Project project:projects){
				int i = 0;
				Sheet sheet = excelWriter.createNewSheet(project.getProjectName());
				Row header = excelWriter.addRowToSheet(sheet, i++);
				excelWriter.addDataToRow(header, projectCsvHeader,"headerRow");
				String[] projectRow = projectToCsvFormat(project);
				Row projectDataRow = excelWriter.addRowToSheet(sheet,i++);
				excelWriter.addDataToRow(projectDataRow, projectRow,"data");
				Vector<Activity> projectActivities = project.getProjectActs();
				Row activityHeader = excelWriter.addRowToSheet(sheet, i++);
				excelWriter.addDataToRow(activityHeader, activityCsvHeader, 1,activityCsvHeader.length-1,"headerRow");
				for(Activity activity:projectActivities){
					String[] activityRow = activityToCsvFormat(activity);
					Row activityDataRow = excelWriter.addRowToSheet(sheet, i++);
			        excelWriter.addDataToRow(activityDataRow, activityRow, 1, activityRow.length-1,"data");
			        Vector<Task> tasks = activity.getActivityTasks();
			        if(tasks != null && tasks.size() > 0 ){
			           Row taskHeader = excelWriter.addRowToSheet(sheet, i++);
			           excelWriter.addDataToRow(taskHeader, taskCsvHeader, 2, taskCsvHeader.length-1,"headerRow");
			        	for(Task task :tasks){
			        	 String[] taskRow = taskToCsvFormat(task);
			        	 Row taskDataRow = excelWriter.addRowToSheet(sheet, i++);
			        	 excelWriter.addDataToRow(taskDataRow, taskRow, 2,taskRow.length-1,"data");
			             Vector<Person> resources = task.getTaskResource();
			             if(resources != null && resources.size() > 0){
			            	 Row resourceHeader = excelWriter.addRowToSheet(sheet, i++);
					         excelWriter.addDataToRow(resourceHeader, resourceCsvHeader, 3, resourceCsvHeader.length-1,"headerRow");
					         for(Person resource :resources){
					        	 String[] resourceRow = resourceToCsv(resource, task);
					        	 Row resourceDataRow = excelWriter.addRowToSheet(sheet, i++);
					        	 excelWriter.addDataToRow(resourceDataRow, resourceRow, 3,resourceRow.length-1,"data");
					         }
			             }
			        	}   
			      }
				}
			 }
			excelWriter.closeExcelWriter();
			Desktop desktop = Desktop.getDesktop();
			File newFile = new File(fileName);
			desktop.open(newFile);
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			System.out.println(e.getMessage());
		}catch (IOException e){
			System.out.println(e.getMessage());
		}
		
	}
    
    private String[] projectToCsvFormat(Project project){
       String[] projectRow = new String[this.projectCsvHeader.length];
       projectRow[0] = project.getProjectID().toString();
       projectRow[1] = project.getProjectName();
       projectRow[2] = project. getProjectDesc();
       projectRow[3] = Common.convertDateToStringFormat(project.getProjectStartDate(),"dd-MM-yyyy");
       return projectRow;
    }
     private String[] activityToCsvFormat(Activity act){
      String[] activityRow = new String[this.activityCsvHeader.length];	
      activityRow[0] = act.getActNumber().toString();
      activityRow[1] = act.getActDesc();
      activityRow[2] = Common.convertDateToStringFormat(act.getActStartDate(),"dd-MM-yyyy");
      activityRow[3] = act.getActDeliver();
      activityRow[4] = act.getHours().toString();
      activityRow[5] = act.getActivityProject().getProjectID().toString();
      return activityRow;
     }
      private String[] taskToCsvFormat(Task task){
    	  String[] taskRow = new String[this.taskCsvHeader.length];
    	  taskRow[0] = task.getTaskNumber().toString();
    	  taskRow[1] = task.getTaskDesc();
    	  taskRow[2] = Common.convertDateToStringFormat(task.getTaskStartDate(),"dd-MM-yyyy");
    	  taskRow[3] = task.getTaskHours().toString();
    	  taskRow[4] = task.getActOfTask().getActNumber().toString();
    	  return taskRow;
      }
      private String[] resourceToCsv(Person p,Task t){
    	  String[] personRow = new String[resourceCsvHeader.length];
    	  personRow[0] = p.getPersonID().toString();
    	  personRow[1] = p.getPersonName();
    	  personRow[2] = p.getPersonGender();
    	  personRow[3] = p.getResourceType();
    	  personRow[4] = t.getTaskNumber().toString();
          return personRow;                 
      }
    
}
